package org.osami.backoffice.eventswitch;

import org.mule.transformers.AbstractTransformer;
import org.mule.umo.transformer.TransformerException;
import org.ow2.jasmine.event.beans.JasmineEventEB;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 19/08/11
 * Time: 11:56
 * To change this template use File | Settings | File Templates.
 */
public class SenseDataMapTransformer extends AbstractTransformer {

    /**
     * Domain name.
     */
    private String domainName;

    /**
     * Server name.
     */
    private String serverName;

    /**
     * The {@link JasmineEventEB#getSname()} attribute.
     */
    private String sname;

    public SenseDataMapTransformer() {
        this.registerSourceType(Map.class);
        this.setReturnClass(JasmineEventEB.class);
    }

    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    @Override
    protected Object doTransform(Object src, String encoding) throws TransformerException {

        Map<String, Serializable> message = (Map<String, Serializable>) src;

        String location = (String) message.get("location");
        String senseId = (String) message.get("sense.id");
        Long timestamp = (Long) message.get("timestamp");
        Double value = (Double) message.get("value");

        // Not used
        String type = (String) message.get("type");
        String unit = (String) message.get("unit");

        return new JasmineEventEB(domainName,
                                  serverName,
                                  location,
                                  senseId + ":" + type,
                                  value,
                                  new Date(timestamp),
                                  sname);
    }
}
